<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();

$adname = $_SESSION["username"];
$client_ip = getClientIP();
$timestamp = time();


if (isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["phone"]) && isset($_POST["email"]) && isset($_POST["imgurl"]) && isset($_POST["group"]) && isset($_POST["adming"]) && isset($_POST["open"])) {
    $username=$_POST["username"];
    $password=$_POST["password"];
    $phone=$_POST["phone"];
    $email=$_POST["email"];
    $imgurl=$_POST["imgurl"];
    $group=$_POST["group"];
    $adming=$_POST["adming"];
    $open=$_POST["open"];
    $repname = $database->select("user_text", "id", ["username" => $username]);
    if ($repname[0] != null) {
        echo "用户名重复!";
        exit();
    }else if($open=="on"){
        if($imgurl==NULL){
            $imgurl="/data/upload/img/default.png";
        }
        $open="是";
        $salt = getSalt();
        $encrypted_data = getPassword($password,$salt);
        $data = $database->insert("user_text",["username"=>$username,"password"=>$encrypted_data,"phone"=>$phone,"email"=>$email,"imgurl"=>$imgurl,"gid"=>$group,"sysgrade"=>$adming,"static"=>$open]);
        $id = $database->select("user_text", "id", ["username" => $username]);
        $database->insert("pass_key", ["uid" => $id[0], "salt" => $salt]);
        $content = $adname."添加了新用户：".$username;
        $database->insert("secure_info", ["user" => $adname, "ip" => $client_ip, "times" => $timestamp, "content" => $content]);
        echo "添加成功!";
    }else{
        echo "参数错误!";
    }
    // 处理数据
}elseif(isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["phone"]) && isset($_POST["email"]) && isset($_POST["imgurl"]) && isset($_POST["group"]) && isset($_POST["adming"])){
    $username=$_POST["username"];
    $password=$_POST["password"];
    $phone=$_POST["phone"];
    $email=$_POST["email"];
    $imgurl=$_POST["imgurl"];
    $group=$_POST["group"];
    $adming=$_POST["adming"];
    if ($repname[0] != null) {
        echo "用户名重复!";
        exit();
    }
    if($imgurl==NULL){
        $imgurl="/data/upload/img/default.png";
    }
    $salt = getSalt();
    $encrypted_data = getPassword($password,$salt);
    $database->insert("user_text",["username"=>$username,"password"=>$encrypted_data,"phone"=>$phone,"email"=>$email,"imgurl"=>$imgurl,"gid"=>$group,"sysgrade"=>$adming]);
    $id = $database->select("user_text", "id", ["username" => $username]);
    $database->insert("pass_key", ["uid" => $id[0], "salt" => $salt]);
    $content = $adname."添加了新用户：".$username;
    $database->insert("secure_info", ["user" => $adname, "ip" => $client_ip, "times" => $timestamp, "content" => $content]);
    echo "添加成功!";
}else{
    echo "参数错误!";
}
?>